package com.yu.newsSys.servlet;

import com.alibaba.fastjson2.JSON;
import com.yu.newsSys.constants.SessionConstants;
import com.yu.newsSys.service.AdminService;
import com.yu.newsSys.service.impl.AdminServiceImpl;
import com.yu.newsSys.utils.CommonUtils;
import com.yu.newsSys.vo.Result;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "admin",urlPatterns = "/admin")
public class AdminServlet extends HttpServlet {

    private AdminService adminService = new AdminServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method.equals("checkUser")) {
            this.checkUser(req,resp);
        } else if (method.equals("zhuce")) {
            this.zhuce(req,resp);
        } else if (method.equals("login")) {
            this.login(req,resp);
        } else if (method.equals("logout")) {
            this.logout(req,resp);
        }
    }
    private void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        session.removeAttribute(SessionConstants.USER_SESSION);
        session.invalidate();
        CommonUtils.printData(Result.OK(),resp);
    }
    // 检验账户名称是否合法
    protected void checkUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("user");

        CommonUtils.logInfo("checkUser","servlet",username);

        Result result = adminService.checkUserName(username);

        CommonUtils.printData(result,resp);
    }
    // 注册功能
    protected void zhuce(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("zhuce from controller running");

        String username = req.getParameter("user");
        String password = req.getParameter("pass");

        Result result = adminService.save(username,password);

        CommonUtils.printData(result,resp);
    }

    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("login from controller running");
        String username = req.getParameter("user");
        String password = req.getParameter("pass");

        Result result = adminService.checkUser(username,password);

        if (result.getCode() == 200) {
            // 将用户信息保存到session中
            req.getSession().setAttribute(SessionConstants.USER_SESSION,username);
        }
        CommonUtils.printData(result,resp);
    }

    // 打印输出流
    private void printDate(Result result,HttpServletResponse resp) throws IOException {
        String resultJsonString = JSON.toJSONString(result);
        resp.setContentType("text/html;charset=utf8");
        ServletOutputStream os = resp.getOutputStream();
        os.write(resultJsonString.getBytes("utf-8"));
    }
}
